//
//  GoodsInfoTableViewCell.m
//  Findif
//
//  Created by Mac on 2018/11/21.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "GoodsInfoTableViewCell.h"


@interface GoodsInfoTableViewCell ()<UIWebViewDelegate>

@property (nonatomic, weak)UILabel * contentLabel;
@property (nonatomic, strong)UIWebView * webView;
@property (nonatomic, copy)NSString * type;
//@property (nonatomic, weak)UIView * lineView;
@property (nonatomic, weak)UIImageView * infoImageView;
@property (nonatomic, weak)UILabel * infoLabel;

@end

@implementation GoodsInfoTableViewCell

- (void)setDict:(NSDictionary *)dict{
    
    _dict = dict;
    
//    self.contentLabel.text = dict[@"merchants_goods_info"];
    NSString *str = dict[@"merchants_goods_info"];
//    str = [self htmlEntityDecode:str];
//    NSAttributedString *attributedStr = [self attributedStringWithHTMLString:str];
//    self.contentLabel.attributedText = attributedStr;
//    [self.webView loadHTMLString:str baseURL:nil];
    
    NSString *htmlString = [NSString stringWithFormat:@"<html> \n"
                            "<head> \n"
                            "<style type=\"text/css\"> \n"
                            "body {font-size:15px;}\n"
                            "</style> \n"
                            "</head> \n"
                            "<body>"
                            "<script type='text/javascript'>"
                            "window.onload = function(){\n"
                            "var $img = document.getElementsByTagName('img');\n"
                            "for(var p in  $img){\n"
                            " $img[p].style.width = '100%%';\n"
                            "$img[p].style.height ='auto'\n"
                            "}\n"
                            "}"
                            "</script>%@"
                            "</body>"
                            "</html>",str];
    
    // webView直接加载HTMLString
    [self.webView loadHTMLString:htmlString baseURL:nil];
    
//    [self.webView loadHTMLString:str baseURL:nil];

}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = ZRWhiteColor;
    
//    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(15 * SCREENH6, 0, 360 * SCREENH6, 1 * SCREENH6)];
//
//    lineView.backgroundColor = ColorWithRGB(0xDBE1E7);
//
//    self.lineView = lineView;
//
//    [self.contentView addSubview:self.lineView];
//    UILabel * contentLabel = [UILabel new];
//
//    contentLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
//    contentLabel.textColor = ZRBlackColor;
//    contentLabel.numberOfLines = 0;
//    self.contentLabel = contentLabel;
//
//    [self.contentView addSubview:self.contentLabel];
    
    UIImageView * infoImageView = [UIImageView new];
    
    infoImageView.image = [UIImage imageNamed:@"商品详情"];
    
    self.infoImageView = infoImageView;
    
    [self.contentView addSubview:self.infoImageView];
    
    UILabel * infoLabel = [UILabel new];
    
    infoLabel.text = @"商品详情";
    infoLabel.textColor = ZRBlackColor;
    infoLabel.font = [UIFont systemFontOfSize:15];
    
    self.infoLabel = infoLabel;
    
    [self.contentView addSubview:self.infoLabel];
    
    self.webView = [[UIWebView alloc]init];
    self.webView.delegate = self;
    self.webView.scrollView.scrollEnabled = NO;
    [self.contentView addSubview:self.webView];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
    CGFloat webViewHeight= [[webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"]floatValue];
//    CGFloat webViewWidth = [[webView stringByEvaluatingJavaScriptFromString: @"document.body.style.width='100%%'"]floatValue];
    
//    NSLog(@"webViewWidth====%f",webViewWidth);
//    NSString * height = [NSString stringWithFormat:@"%f",webViewHeight];
//    NSDictionary * dict = @{@"height":height};
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeCellHeight" object:nil userInfo:dict];
//
//    // CGFloat webViewHeight= [[webView stringByEvaluatingJavaScriptFromString: @"document.body.scrollHeight"]floatValue];
////    CGRect newFrame = webView.frame;
////    newFrame.size.height = webViewHeight;
//    webView.height = webViewHeight;
    
//    NSLog(@"webViewHeight===%f",webViewHeight);
    
//    CGFloat webViewHeight=[webView.scrollView contentSize].height;
    
    if (!self.type) {
        
        NSString * height = [NSString stringWithFormat:@"%f",webViewHeight];
        NSDictionary * dict = @{@"height":height};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeCellHeight" object:nil userInfo:dict];
        self.type = @"asd";
    }
    
//    CGSize contentSize = webView.scrollView.contentSize;
//
//    float rw = 375 * SCREENH6 / contentSize.width;
//
//    webView.scrollView.minimumZoomScale = rw;
//    webView.scrollView.maximumZoomScale = rw;
//    webView.scrollView.zoomScale = rw;
    CGRect newFrame = webView.frame;
    newFrame.size.height = webViewHeight;
//    newFrame.size.width = kScreenW;
    webView.frame = newFrame;
}

- (void)layoutSubviews{
    
    [self.infoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView).offset(15 * SCREENH6);
        make.top.mas_equalTo(self.contentView).offset(10 * SCREENH6);
        make.width.height.mas_equalTo(15 * SCREENH6);
    }];
    
    [self.infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.infoImageView.mas_trailing).offset(5 * SCREENH6);
        make.centerY.mas_equalTo(self.infoImageView);
    }];
    
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.infoImageView.mas_bottom).offset(10 * SCREENH6);
    }];
}



//-(NSString *)filterHTML:(NSString *)html
//{
//        NSScanner * scanner = [NSScanner scannerWithString:html];
//        NSString * text = nil;
//        while([scanner isAtEnd]==NO)
//            {
//                    //找到标签的起始位置
//                    [scanner scanUpToString:@"&" intoString:nil];
//                    //找到标签的结束位置
//                    [scanner scanUpToString:@";" intoString:&text];
//                    //替换字符
//                    html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
//                }
//        //    NSString * regEx = @"<([^>]*)>";
//        //    html = [html stringByReplacingOccurrencesOfString:regEx withString:@""];
//        return html;
//}
////将HTML字符串转化为NSAttributedString富文本字符串
//- (NSAttributedString *)attributedStringWithHTMLString:(NSString *)htmlString
//{
//        NSDictionary *options = @{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,
//                                                                  NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding) };
//        
//        NSData *data = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
//        
//        return [[NSAttributedString alloc] initWithData:data options:options documentAttributes:nil error:nil];
//}
////将 &lt 等类似的字符转化为HTML中的“<”等
//- (NSString *)htmlEntityDecode:(NSString *)string
//{
//        string = [string stringByReplacingOccurrencesOfString:@"&quot;" withString:@"\""];
//        string = [string stringByReplacingOccurrencesOfString:@"&apos;" withString:@"'"];
//        string = [string stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
//        string = [string stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
//        string = [string stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"]; // Do this last so that, e.g. @"&amp;lt;" goes to @"&lt;" not @"<"
//        
//        return string;
//}



- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
